'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('RingRuningLog');

export const GET_LIST_DONE = ns('GET_LIST_DONE');
export const EDIT_ACTIVE = ns('EDIT_ACTIVE');
export const STOP_LOG_SUCCESS = ns('STOP_LOG_SUCCESS');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');

export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function addUserTag(user) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		//web环境执行创建折扣
		ajax({
			  api: 'addUserTagManual',
			  method: 'GET',
			  data:{...user}
			},(json) => {		
				ajax({
					  api: 'addUserTagTask',
					  method: 'GET',
					  data:{...user}
					},(json) => {	
						ajax({
							  api: 'sendUserTagMsg',
							  method: 'GET',
							  data:{sid:window.info.sid,
									tag_id:user.tag_id,
									buyer_nick:encodeURIComponent(user.buyer_nick),
									add_log_id:json.id,
									countdown:user.countdown,
									quere:window.info.vip_group
									}
							},(json) => {	
							  dispatch({
								type: CLOSE_DIALOG,
								visible:false
								});
							},(json) => {
							  //失败后
							  //(json.code==53) W2超时
							  const login = (json.code==210);
							  dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:login
								});
						});
					},(json) => {
					  //失败后
					  //(json.code==53) W2超时
					  const login = (json.code==210);
					  dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
						});
				});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}
}
export function removeLog(log) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		//web环境执行创建折扣
		ajax({
			  api: 'removeUserTagManual',
			  method: 'GET',
			  data:{buyer_nick:log.buyer_nick,tag_id:log.tag_id}
			},(json) => {
						ajax({
						  api: 'stopUserTagTask',
						  method: 'GET',
						  data:{log_id:log.id}
						},(json) => {
						  //成功后
							dispatch({type: STOP_LOG_SUCCESS,id:log.id});
						},(json) => {
						  //失败后
						  //(json.code==53) W2超时
						  const login = (json.code==210);
						  dispatch({
							type: LOAD_ERR,
							message:json.msg+(json.sub_msg?json.sub_msg:""),
							loginFalse:login
						});
					});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}
}

export function getTagLog(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	//web环境执行创建折扣
	ajax({
		  api: 'getTagLog',
		  method: 'GET',
		  data:params
		},(json) => {
		  //成功后
		  json.data = json.data?json.data:[];
			dispatch({
			  type: GET_LIST_DONE,
			  data: json
			});

		},(json) => {
		  //失败后
		  //(json.code==53) W2超时
		  const login = (json.code==210);
		  dispatch({
			type: LOAD_ERR,
			message:json.msg+(json.sub_msg?json.sub_msg:""),
			loginFalse:login
			});
	});
  };
}


